import Vue from 'vue'
import VueRouter from 'vue-router'
import User from "@/views/User.vue";
import Main from "@/views/Main.vue";
import Article from "@/views/Article.vue";
import { Message } from 'element-ui';
import axios from 'axios';
import store from "@/store";
import MyMaterial from "@/views/MyMaterial.vue";
import PublishArticle from "@/views/PublishArticle.vue";
import MyArticle from "@/views/MyArticle";
import NoteData from "@/views/noteData";

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {return originalPush.call(this, location).catch(err => err)}       //解决双击问题

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Main,
    redirect: '/home',
    children: [
      {
        path: "home",
        name: 'home',
        component: () => import('@/views/Home.vue')
      },
      {
        path: 'article',
        name: 'article',
        component: Article
      },
      {
        path: 'user',
        name: 'user',
        component: User
      },
      {
        path: 'page1',
        name: 'page1',
        component: () => import('@/views/PageOne.vue')
      },
      {
        path: 'page2',
        name: 'page2',
        component: () => import('@/views/PageTwo.vue')
      },
      {
        path: "myMaterial",
        name:'myMaterial',
        component:()=>import("@/views/MyMaterial.vue")
      },
      {
        path: "publishArticle",
        name:"publishArticle",
        component:PublishArticle
      },
      {
        path:"myArticle",
        name:"myArticle",
        component:MyArticle
      },
      {
        path: "noteData",
        name: "noteData",
        component:NoteData
      }
    ]
  },
  {
    path:'/login',
    name:'login',
    component: () => import('@/views/Login.vue')
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
const axiosInstance = axios.create({});
function judgeToken(token) {

}
router.beforeEach((to, from, next) => {
  console.log(to.path + " " + from.path)
  if(to.path === '/login'){
    next();
  }else{
    let token = localStorage.getItem('token');
    if(token === null || token === ''){
      next('/login')
      Message({
        type: 'error',
        message: '请先登录'
      })
    }else{
      axiosInstance.get('http://115.159.84.44:24576/user/backend/v1/user/getMenuList', {
        headers:{
          'token': localStorage.getItem("token")
        }
      }).then((response) => {
        if(response.data.code === 200){
          console.log("token未过期");
          next()
        }else{
          next('/login')
        }
      })
    }
  }

  if (to.path === from.path) {

    next(false); // 取消路由跳转
  } else {
    next(); // 允许路由跳转
  }
});

// router.onError((err) => {
//   if (err.name === 'NavigationDuplicated' && err.message.includes('redundant navigation')) {
//     // 静默处理该错误，不打印报错信息
//   } else {
//     // 其他错误，执行默认的错误处理逻辑
//     // console.error(err);
//   }
// });
export default router
